//
//  IDVerificationViewController.swift
//  YLH
//
//  Created by qinjiangtao on 2024/1/12.
//  绑定手机号和身份验证页面

import UIKit

class IDVerificationViewController: QIBaseViewController {
    
    var isVerification = false
    private var codeTextF = UITextField()
    private var getCodeBtn = GSCaptchaButton()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        if isVerification {
            title = "身份验证"
        } else {
            title = "绑定手机号"
        }
        
        addUI()
        getCodeBtnClick()
    }
    

    
    private func addUI() {
        
        let titleL = UILabel().then({
            $0.frame = CGRect(x: 20, y: NAV_HEIGHT + 20, width: SCREEN_WIDTH - 40, height: 20)
            $0.text = "请在5分钟内输入\(QIUserModel.shared().mobile)收到的验证码"
            $0.textColor = .black
            $0.font = SYSFONTBOLD(ofSize: 14)
        })
        view.addSubview(titleL)
        
        codeTextF = UITextField().then {
            $0.frame = CGRect(x: 20, y: titleL.bottom + 20, width: 200, height: 40)
            $0.font = SYSFONT(ofSize: 16)
            $0.placeholder = "请输入验证码"
        }
        view.addSubview(codeTextF)
        
        getCodeBtn = GSCaptchaButton().then {
            $0.frame = CGRect(x: SCREEN_WIDTH - 140, y: codeTextF.top, width: 120, height: codeTextF.height)
            $0.titleLabel?.font = SYSFONT(ofSize: 15)
            $0.setTitle("获取验证码", for: .normal)
            $0.setTitle("重新获取：second秒", for: .disabled)
            $0.setTitleColor(MainColor, for: .normal)
            $0.addTarget(self, action: #selector(getCodeBtnClick), for: .touchUpInside)
        }
        view.addSubview(getCodeBtn)
        
        let lineView = UIView()
        lineView.frame = CGRect(x: 20, y: codeTextF.bottom + 10, width: SCREEN_WIDTH - 40, height: 1)
        lineView.backgroundColor = ColorEEEEEE
        view.addSubview(lineView)
        
        let nextBtn = UIButton().then {
            $0.frame = CGRect(x: 20, y: lineView.bottom + 50, width: SCREEN_WIDTH - 40, height: 44)
            $0.backgroundColor = MainColor
            $0.titleLabel?.font = SYSFONT(ofSize: 14)
            $0.setTitle("下一步", for: .normal)
            $0.setTitleColor(.white, for: .normal)
            $0.addRoundedCorners(withRadius: 22)
            $0.addTarget(self, action: #selector(nextBtnClick), for: .touchUpInside)
        }
        view.addSubview(nextBtn)
        
        let deactivateBtn = UIButton().then {
            $0.frame = CGRect(x: 20, y: nextBtn.bottom + 10, width: nextBtn.width, height: 40)
            $0.titleLabel?.font = SYSFONT(ofSize: 14)
            $0.setTitle("手机号已停用", for: .normal)
            $0.setTitleColor(MainColor, for: .normal)
            $0.addTarget(self, action: #selector(deactivateBtnClick), for: .touchUpInside)
        }
        view.addSubview(deactivateBtn)
        
        if isVerification {
            let mobileStr = "\(QIUserModel.shared().mobile)"
            let replacedPhoneNumber = String(mobileStr.dropLast(4)) + "****" + String(mobileStr.suffix(4))
            let prefix = mobileStr.prefix(3)
            let suffix = mobileStr.suffix(4)

            titleL.text = "提示: \(prefix)****\(suffix)"
            getCodeBtn.isHidden = true
            deactivateBtn.isHidden = true
            codeTextF.placeholder = "请输入完整手机号"
            
        }
    }
    
    @objc private func getCodeBtnClick() {
        
        let param = [
            "event": "validatemobile",
            "mobile": QIUserModel.shared().mobile
        ]
        QINetworkManager.requestJson(urlStr: "/Sms/send", parameters: param) { [self] json in
            getCodeBtn.maxSecond = 60
            getCodeBtn.countdown = true
        }
    }
    @objc private func nextBtnClick() {
        if isVerification {
            let mobileStr = codeTextF.text ?? ""
            if mobileStr == QIUserModel.shared().mobile {
                let ctl = MyBindingPhone2ViewController()
                navigationController?.pushViewController(ctl, animated: true)
            } else {
                ShowHUD(str: "您输入的手机号错误")
            }
        } else {
            let mobileStr = codeTextF.text ?? ""
            guard mobileStr.count > 0 else {
                ShowHUD(str: "请输入验证码")
                return
            }
            let param = [
                "captcha": mobileStr,
                "mobile": QIUserModel.shared().mobile
            ]
            QINetworkManager.requestJson(urlStr: "/User/validateMobile", parameters: param) { json in
                let ctl = MyBindingPhone2ViewController()
                self.navigationController?.pushViewController(ctl, animated: true)
            }
        }
    }
    @objc private func deactivateBtnClick() {
        let ctl = IDVerificationViewController()
        ctl.isVerification = true
        navigationController?.pushViewController(ctl, animated: true)
    }

}
